#coding:utf-8
from django.contrib import admin
from lovewith.promote.models import *


#类目推荐关键词管理
class PromoteCateTagAdmin(admin.ModelAdmin):
    list_display = ('tag', 'link', 'sort', 'cate')
    ordering = ('sort',)
    #过滤器
    list_filter = ('cate',)


#首页轮播图片
class PromoteHomeCycleAdmin(admin.ModelAdmin):
    list_display = ('id', 'image_path', 'promote_title', 'content', 'type')
    ordering = ('-id', )
    #过滤器
    list_filter = ('type',)

    #图片
    def image_path(self, obj):
        img_path = ''
        if obj.banner.name.find('http') < 0:
            img_path = 'http://img.lovewith.me/static/upload/%s' % obj.banner.name
        else:
            img_path = obj.banner

        return '<img src="%s" width="100" />' % img_path

    image_path.short_description = '图片预览'
    image_path.allow_tags = True

    #描述
    def promote_title(self, obj):
        return '%s...' % obj.title[0:25]

    promote_title.short_description = '标题'

    #描述
    def content(self, obj):
        return '%s...' % obj.description[0:30]

    content.short_description = '描述'

    #标题链接
    def title_link(self, obj):
        return '<a href="%s" target="_blank">%s</a>' % (self.link, obj.title)

    title_link.short_description = '标题'
    title_link.allow_tags = True


class PromoteHomeChoiceAdmin(admin.ModelAdmin):
    list_display = ('id', 'topic_title', 'topic_content')
    ordering = ('-id', )
    search_fields = ('title',)

    def topic_title(self, obj):
        return '%s...' % obj.title[0:25]
    topic_title.short_description = '标题'

    def topic_content(self, obj):
        return obj.content
    topic_content.short_description = '标签'
    topic_content.allow_tags = True


#商品推荐标签
class PromoteGoodsCommandTagAdmin(admin.ModelAdmin):
    list_display = ('id', 'tags')
    ordering = ('-id',)


#订阅
class CommandSubscribeAdmin(admin.ModelAdmin):
    list_display = ('id', 'keyword', 'banner_image')
    ordering = ('sort',)

    #图片
    def banner_image(self, obj):
        img_path = '%s!100' % obj.banner

        return '<img src="/static/upload/%s" width="60" />' % obj.banner

    banner_image.short_description = '图片预览'
    banner_image.allow_tags = True


#推荐灵感板
class PromoteHomeAlbumAdmin(admin.ModelAdmin):
    list_display = ('id', 'album')
    ordering = ('-id',)


#首页栏目
class PromoteHomeColumnAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'sort')
    ordering = ('-sort',)


#注册显示model
admin.site.register(PromoteGoodsCommandTag, PromoteGoodsCommandTagAdmin)
admin.site.register(PromoteCateTag, PromoteCateTagAdmin)
admin.site.register(PromoteHomeCycle, PromoteHomeCycleAdmin)
admin.site.register(PromoteHomeChoice, PromoteHomeChoiceAdmin)
admin.site.register(CommandSubscribe, CommandSubscribeAdmin)
admin.site.register(PromoteHomeAlbum, PromoteHomeAlbumAdmin)
admin.site.register(PromoteHomeColumn, PromoteHomeColumnAdmin)